<!DOCTYPE html>
<html>
	<head>
		<title>商品分类-详情</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css js 资源 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css"> 
		<script src="https://unpkg.com/vue@2.6.10/dist/vue.js"></script>
		<script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>
		<script src="https://unpkg.com/http-vue-loader@1.4.2/src/httpVueLoader.js"></script>
		<script src="https://unpkg.com/jquery@3.4.1/dist/jquery.js"></script>
		<script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>
		<script src="../../static/sa.js"></script>
		<style type="text/css">
			.c-panel .c-label{width: 8em;}
			.c-item .image-box-2{height: 90px;}
		</style>
	</head>
	<body>
		<div class="vue-box sbot" style="display: none;" :style="'display: block;'">
			<!-- ------- 内容部分 ------- -->
			<div class="s-body">
				<div class="c-panel">
					<el-form v-if="m">
						<sa-info name="商品ID" br>{{m.id}}</sa-info>
						<sa-info name="商品名称" br>{{m.name}}</sa-info>
						<sa-info name="商品描述" br>{{m.description}}</sa-info>
						<sa-info name="价格" br>¥{{m.price}}</sa-info>
						<sa-info name="库存" br>{{m.stock}}</sa-info>
						<sa-info name="状态" br>
							<el-tag :type="m.isActive ? 'success' : 'info'">{{m.isActive ? '在售' : '下架'}}</el-tag>
						</sa-info>
						<sa-info name="分类" br>{{m.categoryName}}</sa-info>
						<sa-info name="品牌" br>{{m.brandName}}</sa-info>
						
						<sa-info name="商品图片" br>
							<div class="image-list">
								<el-image 
									v-for="img in m.images" 
									:key="img.image_id"
									:src="getFullImageUrl(img.image_url)"
									:preview-src-list="getImageUrlList()"
									fit="cover"
									class="product-image">
								</el-image>
							</div>
						</sa-info>
						
						<sa-info name="SKU信息" br>
							<el-table :data="m.skus" border size="mini">
								<el-table-column prop="sku_code" label="SKU编码" width="180"></el-table-column>
								<el-table-column prop="sku_price" label="价格" width="100">
									<template slot-scope="scope">¥{{scope.row.sku_price}}</template>
								</el-table-column>
								<el-table-column prop="sku_stock" label="库存"></el-table-column>
							</el-table>
						</sa-info>
						
						<sa-info name="商品属性" br>
							<el-table :data="attributeTableData" border style="width: 100%">
								<el-table-column prop="name" label="属性名称" width="150"></el-table-column>
								<el-table-column prop="value" label="属性值">
									<template slot-scope="scope">
										<div v-if="scope.row.values.length === 1">
											{{scope.row.values[0]}}
										</div>
										<div v-else>
											<el-tag v-for="(value, index) in scope.row.values" :key="index" style="margin-right: 5px; margin-bottom: 5px;">
												{{value}}
											</el-tag>
										</div>
									</template>
								</el-table-column>
							</el-table>
						</sa-info>
					</el-form>
				</div>
			</div>
			<!-- ------- 底部按钮 ------- -->
			<div class="s-foot">
				<el-button @click="sa.closeCurrIframe()" type="success">确定</el-button>
				<el-button @click="sa.closeCurrIframe()">取消</el-button>
			</div>
		</div>
		<script src="./moke-data.js"></script>
		<script>
			var app = new Vue({
				components: {
					"sa-info": httpVueLoader('../../sa-frame/com/sa-info.vue'),		
				},
				el: '.vue-box',
				data: {
					id: sa.p('id', 0),	// 获取数据ID 
					m: null,
					baseImageUrl: 'http://8.134.184.130:8008/images/' // 图片基础URL
				},
				methods: {
					// 获取完整图片URL
					getFullImageUrl: function(path) {
						if (!path) return '';
						if (path.startsWith('http://') || path.startsWith('https://')) {
							return path;
						}
						return this.baseImageUrl + path;
					},
					// 获取所有图片URL列表用于预览
					getImageUrlList: function() {
						if (!this.m || !this.m.images) return [];
						return this.m.images.map(img => this.getFullImageUrl(img.image_url));
					}
				},
				computed: {
					// 将属性按名称分组
					groupedAttributes: function() {
						if (!this.m || !this.m.attributes) return {};
						return this.m.attributes.reduce((groups, attr) => {
							if (!groups[attr.attribute_name]) {
								groups[attr.attribute_name] = [];
							}
							groups[attr.attribute_name].push(attr);
							return groups;
						}, {});
					},
					// 转换为表格数据格式
					attributeTableData: function() {
						if (!this.m || !this.m.attributes) return [];
						const grouped = this.groupedAttributes;
						return Object.keys(grouped).map(name => ({
							name: name,
							values: grouped[name].map(attr => attr.attribute_value)
						}));
					}
				},
				mounted: function() {
					$.ajax({
						url: 'http://localhost:8080/product/getProductById?id=' + this.id,
						type: 'GET',
						success: function(res) {
							this.m = res.data;
						}.bind(this)
					});
				}
			})
			
			// 获取对应的data 
			function getById(id) {
				var mockData = getMockData();
				var product = mockData.data.list.find(function(item) {
					return item.id == id;
				});
				
				return {
					code: 200,
					msg: 'ok',
					data: product
				};
			}
			function getMockData() {
				var id = sa.p('id', 0);	
				var data = null;
				// 遍历判断 
				mockData.data.forEach(function(item) {
					if(item.id == id) {
						data = item;
					}
				})
				// 创建模拟数据 
				var mockData = {
					code: 200,
					msg: 'ok',
					data: data
				}
				return mockData;
			}
			function getMockData() {
				var id = sa.p('id', 0);	
				var data = null;
				// 遍历判断 
				mockData.data.forEach(function(item) {
					if(item.id == id) {
						data = item;
					}
				})
				// 创建模拟数据 
				var mockData = {
					code: 200,
					msg: 'ok',
					data: data
				}
				return mockData;
			}
		</script>
		<style>
			.c-panel .c-label{width: 8em;}
			.image-list {
				display: flex;
				gap: 10px;
				flex-wrap: wrap;
			}
			.product-image {
				width: 100px;
				height: 100px;
				border-radius: 4px;
				border: 1px solid #eee;
			}

			.el-table--mini {
				width: 100%;
			}
		</style>
	</body>
</html>
